package com.lc.user.express.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.model.PreferentialModel;

import java.util.List;

/**
 * Created by Administrator on 2016/3/21.
 */
public class PreferentialAdapter extends BaseListAdapter<PreferentialModel> {
    public PreferentialAdapter(Context context, List<PreferentialModel> data) {super(context, data);}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        PreferentialModel model=data.get(position);
        if (convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.preferen_list_item,null);
            AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
            viewHolder=new ViewHolder();
            viewHolder.iv_pic= (ImageView) convertView.findViewById(R.id.iv_pic);
            viewHolder.tv_title = (TextView)convertView.findViewById(R.id.tv_title);
            convertView.setTag(viewHolder);
        }
        setContextView(convertView,model);

        return convertView;
    }

    private void setContextView(View convertView, PreferentialModel model) {
        ViewHolder viewHolder= (ViewHolder) convertView.getTag();
     //   ImageManager.from(mContext).displayImage(viewHolder.iv_pic,model.getPicUrl(),R.drawable.shape_background);
        Glide.with(mContext)
                .load(model.getPicUrl())
                .placeholder(R.drawable.shape_background) // can also be a drawable
                .error(R.drawable.shape_background) // will be displayed if the image cannot be loaded
                .crossFade()
                .into(viewHolder.iv_pic);
        viewHolder.tv_title.setText(model.getTitle());
    }

    private  class ViewHolder{
        ImageView iv_pic;
        TextView tv_title;
    }
}
